package com.nsn.auction.domina;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * Represents comment set by one user for another.
 * @author mdaniele
 *
 */

@Entity
@Table(name="COMMENT")
public class Comment implements Serializable{
	
	private Long commentId;
	private User sender;
	private User recipient;
	private StringBuffer matter;
	private Date date;
	private CommentType commentType;

	public Comment() {}
	
	public Comment(User sender, User recipient, CommentType commentType) {
		this.sender = sender;
		this.recipient = recipient;
		this.commentType = commentType;
	}
	
	public Comment(User sender, User recipient, Date date, StringBuffer matter, CommentType commentType) {
		this.sender = sender;
		this.recipient = recipient;
		this.date = date;
		this.matter = matter;
		this.commentType = commentType;
	}
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name="COMMENT_ID", unique = true, nullable = false)
	public Long getCommentId() {
		return commentId;
	}
	
	public void setCommentId(Long commentId) {
		this.commentId = commentId;
	}
	
	@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	public User getSender() {
		return sender;
	}
	
	public void setSender(User sender) {
		this.sender = sender;
	}
	
	@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	public User getRecipient() {
		return recipient;
	}
	
	public void setRecipient(User recipient) {
		this.recipient = recipient;
	}
	
	@Column(name="MATTER", nullable = false, length = 100)
	public StringBuffer getMatter() {
		return matter;
	}
	
	public void setMatter(StringBuffer matter) {
		this.matter = matter;
	}
	
	@Temporal(TemporalType.DATE)
	@Column(name="DATE", unique = true, nullable = false, length = 20)
	public Date getDate() {
		return date;
	}
	
	public void setDate(Date date) {
		this.date = date;
	}
	
	@Enumerated(EnumType.STRING)
	@Column(name="COMMENT_TYPE", nullable = false)
	public CommentType getCommentType() {
		return commentType;
	}

	public void setCommentType(CommentType commentType) {
		this.commentType = commentType;
	}
	
	@Override
	public boolean equals(Object object) {
		
		//check for selt-comparison
		if (this == object) return true;
		if (!( this.getClass().equals(object.getClass()) )) return false;
		
		Comment temp_comment = (Comment) object;
		 return (null == getSender() ? null == temp_comment.getSender() : getSender().equals(temp_comment.getSender()) && 
				null == getRecipient() ? null == temp_comment.getRecipient() : getRecipient().equals(temp_comment.getRecipient()) && 
				null == getMatter() ? null == temp_comment.getMatter() : getMatter().toString().equals(temp_comment.getMatter().toString()) &&
				null == getCommentType() ? null == temp_comment.getCommentType() : getCommentType().equals(temp_comment.getCommentType()));
	}
	
}

